﻿;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Logistics.Route", [])
        .controller("LogisticsRouteCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$timeout", "$filter", "EventBus", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $timeout, $filter, EventBus, UserContextService) {
            $scope.activeFlag = [true, false, false];
            $scope.tabFlag = {};
            $scope.carrierList = [];//承运商列表
            $scope.condition = {};
            $scope.dataList = [];
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.submitted = false;
            var tabFlag = {
                0: "新增",
                1: "编辑"
            };
            $scope.toolbarConfig = {
                showAdvanceSearch: false,
                showColConfig: false
            };
            $scope.tableConfig = {
                scrollWidth: 1240,
                fixedWidth: 160,
                tableCol: [
                    {
                        type: "",
                        name: "线路产品ID",
                        dataField: "id",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                        clickFn: "viewDetail"
                    },
                    {
                        type: "",
                        name: "承运商",
                        dataField: "carrierOrgName",
                        width: 100,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        type: "",
                        name: "出发城市",
                        dataField: "departureCity",
                        width: 150,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        type: "",
                        name: "目的城市",
                        dataField: "destinationCity",
                        width: 150,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        type: "",
                        name: "里程",
                        dataField: "mileage",
                        width: 80,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        type: "",
                        name: "承诺时间(天)",
                        dataField: "commitmentTime",
                        width: 100,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        type: "",
                        name: "创建时间",
                        dataField: "createDateShow",
                        width: 130,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        type: "",
                        name: "规则描述",
                        dataField: "ruleDes",
                        width: 130,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        type: "operate",
                        name: "操作",
                        dataField: "",
                        width: 80,
                        isShow: true,
                        isFixed: true
                    }
                ]
            };
            $scope.markInfo = {
                allChecked: false
            };

            $scope.detailInfo = {
                ruleDes:"",
                carrierOrgName: "",
                carrierOrgId: "",
                departureCity: "",
                destinationCity: "",
                mileage:"",
                commitmentTime: "",
                firstWeight:"",
                firstAmount:"",
                list:[{
                    weightStart: "",
                    weightEnd: "",
                    unitPrice: "",
                    additional:""

                }]
            };

            var btnPermissionCode = {
                routeBillAdd: window.ActionCode.routeAdd,
                routeBillEdit: window.ActionCode.routeEdit,
                routeBillDelete: window.ActionCode.routeDel
            };

            $scope.btnPermission = {
                routeBillAdd: true,
                routeBillEdit: true,
                routeBillDelete: true
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.reset = function () {
                $scope.condition = {};
            };

            function getDataList(curPage) {
                var service = "device";
                var reqUrl = "/logisticsBasic/list";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = angular.copy($scope.condition);
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                    angular.forEach($scope.dataList, function (item) {
                        item.createDateShow = $filter("YbDate")(item.createDate,'yyyy-MM-dd HH:mm:ss');
                    });
                });
            }

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.addData = function () {
                $scope.tabFlag.type = 0;
                $scope.tabFlag.name = tabFlag[0];
                $scope.detailInfo = {
                    ruleDes:"",
                    carrierOrgName: "",
                    carrierOrgId: "",
                    departureCity: "",
                    destinationCity: "",
                    mileage:"",
                    commitmentTime: "",
                    firstWeight:"",
                    firstAmount:"",
                    list:[{
                        weightStart: "",
                        weightEnd: "",
                        unitPrice: "",
                        additional:""
    
                    }]
                };
                $scope.changeTab(1);
            };

            $scope.saveData = function () {
                var param = $scope.detailInfo;
                filterEmptyList();
                param.mileage = +param.mileage;
                param.commitmentTime = +param.commitmentTime;
                var service = "device";
                var reqUrl = "/logisticsBasic/add";
                if ($scope.tabFlag.type === 1) {
                    reqUrl = "/logisticsBasic/update";
                }
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    getDataList();
                    $scope.changeTab(0);
                });
            };

            $scope.changeTab = function (index) {
                $scope.activeFlag = [false, false, false];
                $scope.activeFlag[index] = true;
                $scope.submitted = false;
                switch (index) {
                    case 0:
                        break;
                    case 1:
                        break;
                    case 2:
                        break;
                    default:
                        break;
                }
            };

            $scope.tdClick = function (item, fnType) {
                switch (fnType) {
                    case "viewDetail":
                        viewDetail(item);
                        break;
                    default:
                        break;
                }
            };
            function viewDetail(item) {
                $scope.detailInfo = item;
                $scope.changeTab(2);
            }

            $scope.setFixedWidth = function () {
                $scope.tbStyle = {scrollLeft: {left: $scope.tableConfig.fixedWidth + "px"}};
            };

            $scope.toSort = function (config, index) {
                ybWidget.ToSort(config, index);
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.cueRow = index
            };

            $scope.editData = function (item) {
                getRouteItemInfo(item).then(function (data) {
                    $scope.detailInfo = data;
                    $scope.tabFlag.type = 1;
                    $scope.tabFlag.name = tabFlag[1];
                    $scope.changeTab(1);
                })
            };
            
            $scope.deleteData = function (item) {
                var service = "device";
                var reqUrl = "/logisticsBasic/delete";
                var param = {};
                param.ids = [];
                param.ids.push(item.id);
                ybWidget.ShowMessage("确定要删除吗？", MESG_TYPE.Ask).then(function () {
                    ybHttp.Post(service, reqUrl, param).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        getDataList();
                    })
                }, function () {

                })
            };

            $scope.selectLogisticsCom = function () {
                selectCarrier().then(function (data) {
                    $scope.condition.carrierOrgName = data.carrierOrgName;
                    $scope.condition.carrierOrgId = data.carrierOrgId;
                });
            };
            $scope.selectLogisticsCarrier = function () {
                selectCarrier().then(function (data) {
                    $scope.detailInfo.carrierOrgName = data.carrierOrgName;
                    $scope.detailInfo.carrierOrgId = data.carrierOrgId;
                });
            };
            
            function selectCarrier() {
                var param = {
                    name: "承运物流",
                    type: "carrier",
                    eventStop:"true"
                };
                return ybWidget.SingleSelector(param).then(function (data) {
                    return {
                        carrierOrgName: data.companyName,
                        carrierOrgId: data.orgId
                    };
                })
            }

            $scope.minusCustodian = function (index, list) {
                if(index < list.length) {
                    list.splice(index,1);
                }
            };
            $scope.plusCustodian = function (list) {
                var item = {
                    weightStart: "",
                    weightEnd: "",
                    unitPrice: ""
                };
                list.push(item);
            };
            
            function filterEmptyList() {
                for(var i = $scope.detailInfo.list.length-1; i >= 0; i--) {
                    var item = $scope.detailInfo.list[i];
                    if ((item.weightEnd == "") && (item.weightStart === "") && (item.unitPrice === "")) {
                        $scope.minusCustodian(i, $scope.detailInfo.list);
                    }else {
                        item.weightEnd = +item.weightEnd;
                        item.weightStart = +item.weightStart;
                        item.unitPrice = +item.unitPrice;
                        item.additional = +item.additional;
                    }
                }
            }

            function getRouteItemInfo(item) {
                var service = "device";
                var reqUrl = "/logisticsBasic/item";
                var param = {
                    id: item.id
                };
               return  ybHttp.Get(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    return resp.value;
                });
            }


            (function () {
                getDataList();
                $scope.setFixedWidth();
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }])
    ;
})(window, window.angular);